package com.lcxw.hxzbadmin.controller.giftandcubecontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbadmin.entity.requestbean.GetGiftReceiveRecordReq;
import com.lcxw.hxzbadmin.entity.requestbean.GetGiftSendRecordReq;
import com.lcxw.hxzbadmin.entity.requestbean.RequestBodyBasic;
import com.lcxw.hxzbadmin.entity.responseBean.GetGiftReceiveRecordResp;
import com.lcxw.hxzbadmin.entity.responseBean.GetGiftSendRecordResp;
import com.lcxw.hxzbapi.entity.pojo.Gift;
import com.lcxw.hxzbapi.entity.pojo.GiftSend;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.GiftSendService;
import com.lcxw.hxzbapi.service.GiftService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.var;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/gift")
public class GiftSendGetController {

    @Resource
    GiftSendService giftSendService;

    @Resource
    GiftService giftService;

    @Resource
    YwpdMemberService ywpdMemberService;

    /**
     * 获取礼物赠送记录的用户列表
     */
    @PostMapping("/send/getRecords")
    public Response getGiftSendMembers(@RequestBody GetGiftSendRecordReq requestBody) {
        int pageNum = requestBody.getPageNum();
        int pageSize = requestBody.getPageSize();
        pageNum = (pageNum - 1) * pageSize;
        long fromUserId = requestBody.getUserId();

        int count = giftSendService.selectCountByfromUserId(fromUserId);

        List<GiftSend> sendRecords = giftSendService
            .selectByFromUserIdWithPage(fromUserId, pageNum, pageSize);

        List<GetGiftSendRecordResp> giftSendRecordRespList = new ArrayList<>();
        for (GiftSend giftSend : sendRecords
        ) {
            GetGiftSendRecordResp getGiftSendRecordResp = new GetGiftSendRecordResp();
            long userId = giftSend.getUserId();
            int giftId = giftSend.getGiftId();
            YwpdMember ywpdMember = ywpdMemberService.selectYwpdMemberByUserId(userId);
            Gift gift = giftService.selectByPrimaryKey(giftId);
            getGiftSendRecordResp.setUserName(ywpdMember.getUserName());
            getGiftSendRecordResp.setNickName(ywpdMember.getNickName());
            getGiftSendRecordResp.setGiftName(gift.getGiftName());
            getGiftSendRecordResp.setGiftNum(giftSend.getGiftNum());
            getGiftSendRecordResp.setAddTime(giftSend.getAddTime());
            giftSendRecordRespList.add(getGiftSendRecordResp);
        }
        JSONObject data = new JSONObject();
        data.put("total", count);
        data.put("giftSendRecordRespList", giftSendRecordRespList);
        return Response.ok(data);
    }

    /**
     * 礼物收取记录的查询接口
     */
    @PostMapping("/receive/getRecords")
    public Response getGiftReceiveRecords(
        @RequestBody GetGiftReceiveRecordReq getGiftReceiveRecordReq) {
        int pageNum = getGiftReceiveRecordReq.getPageNum();
        int pageSize = getGiftReceiveRecordReq.getPageSize();
        pageNum = (pageNum - 1) * pageSize;
        long userId = getGiftReceiveRecordReq.getUserId();

        int count = giftSendService.selectCountByUserId(userId);

        List<GiftSend> sendRecords = giftSendService
            .selectByUserIdWithPage(userId, pageNum, pageSize);

        List<GetGiftReceiveRecordResp> getGiftReceiveRecordRespList = new ArrayList<>();
        for (GiftSend giftSend : sendRecords
        ) {
            GetGiftReceiveRecordResp getGiftReceiveRecordResp = new GetGiftReceiveRecordResp();
            long fromUserId = giftSend.getFromUserId();
            int giftId = giftSend.getGiftId();
            YwpdMember ywpdMember = ywpdMemberService.selectYwpdMemberByUserId(fromUserId);
            Gift gift = giftService.selectByPrimaryKey(giftId);
            getGiftReceiveRecordResp.setUserName(ywpdMember.getUserName());
            getGiftReceiveRecordResp.setNickName(ywpdMember.getNickName());
            getGiftReceiveRecordResp.setGiftName(gift.getGiftName());
            getGiftReceiveRecordResp.setGiftNum(giftSend.getGiftNum());
            getGiftReceiveRecordResp.setAddTime(giftSend.getAddTime());
            getGiftReceiveRecordRespList.add(getGiftReceiveRecordResp);
        }
        JSONObject data = new JSONObject();
        data.put("total", count);
        data.put("giftReceiveRecordRespList", getGiftReceiveRecordRespList);
        return Response.ok(data);

    }
}
